﻿using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using Umbraco.Cms.Web.EmbeddedViewEngine;
using Umbraco.Cms.Web.Model.BackOffice.PropertyEditors;
using Umbraco.Framework;

namespace Umbraco.Cms.Web.PropertyEditors.UserPicker
{
	[EmbeddedView("Umbraco.Cms.Web.PropertyEditors.UserPicker.Views.UserPickerEditor.cshtml", "Umbraco.Cms.Web.PropertyEditors")]
	public class UserPickerEditorModel : EditorModel<UserPickerPreValueModel>
	{
		public UserPickerEditorModel(UserPickerPreValueModel preValueModel)
			: base(preValueModel)
		{
		}

		public HiveId? UserId { get; set; }

		[ScaffoldColumn(false)]
		[ReadOnly(true)]
		public IEnumerable<SelectListItem> Users { get; set; }

		public override IDictionary<string, object> GetSerializedValue()
		{
			var val = new Dictionary<string, object>();
			
			if (UserId != null && !UserId.Value.IsNullValueOrEmpty())
			{
				val.Add("UserId", UserId.ToString());
			}

			return val;
		}

		public override void SetModelValues(IDictionary<string, object> serializedVal)
		{
			if (serializedVal.ContainsKey("UserId"))
			{
				UserId = HiveId.Parse((string)serializedVal["UserId"]);
			}
		}
	}
}